package com.simba.server.commands.impl.record;

import com.simba.common.command.ICommand;
import com.simba.common.database.DbOperator;
import com.simba.server.base.CommonUtil;
import com.simba.server.components.data.SortInfo;

/**
 * @author create by kavin
 * @date 2020/11/1 0:54
 */
public class RecordPacketOffReportCmd implements ICommand {
    public static final String NAME = "RecordPacketOffReportCmd";

    private final String columns = "" +
            "barcode,platId,droppedSlot,serverSlots,roadSlot,roadSlotName,errorCodeSVR,errorCodePLC,obrNo,boxCode," +
            "expiredFlag,scannedTime,packetOffTime";

    private final SortInfo sortInfo;
    private final String sql = "insert into tt_packet_off_info_report(" + columns + ") values" +
            "(?,?,?,?,?,?,?,?,?,?,?,?,?);";

    public RecordPacketOffReportCmd(SortInfo sortInfo)
    {
        this.sortInfo = sortInfo;
    }

    @Override
    public String getName()
    {
        return RecordPacketOffReportCmd.NAME;
    }

    @Override
    public Boolean run(Object params)
    {
        boolean ret = false;
        if (!(params instanceof DbOperator))
        {
            return ret;
        }
        try
        {

            DbOperator dbOperator = (DbOperator) params;
            dbOperator.prepareStatement(this.sql);

            dbOperator.setString(1,  CommonUtil.doNullData(this.sortInfo.getBarcode(), "none"));
            dbOperator.setString(2,  CommonUtil.doNullData(this.sortInfo.getSorterId(), "none"));
            dbOperator.setInt(3,  this.sortInfo.getDroppedSlotId());
            dbOperator.setString(4,  CommonUtil.doNullData(this.createServerSlots(), "none"));
            dbOperator.setString(5,  CommonUtil.doNullData(this.sortInfo.getRoadSlot(), "none"));
            dbOperator.setString(6,  CommonUtil.doNullData(this.sortInfo.getRoadSlotName(), "none"));
            dbOperator.setInt(7, this.sortInfo.getErrorCodeSVR());
            dbOperator.setInt(8, this.sortInfo.getErrorCodePLC());
            dbOperator.setInt(9, this.sortInfo.getObr());
            dbOperator.setString(10,  CommonUtil.doNullData(this.sortInfo.getBoxCode(), "none"));
            dbOperator.setInt(11,  0);
            dbOperator.setString(12, CommonUtil.doNullData(this.sortInfo.getTimePacketOn(), "none"));
            dbOperator.setString(13, CommonUtil.doNullData(this.sortInfo.getTimePacketOff(), "none"));

            int res = dbOperator.executeUpdate();

            if (res > 0)
            {
                ret = true;
            }
            else
            {
                ret = false;
            }

        }
        catch (Exception ex)
        {
            ret = false;
        }
        return ret;
    }

    private String createServerSlots()
    {
        String serverSlots;
        try
        {
            StringBuilder slotsContent = new StringBuilder();
            int slotNum = this.sortInfo.getSlotsList().size();
            for (int i = 0; i < slotNum; i++)
            {
                if (i != slotNum - 1)
                {
                    slotsContent.append(this.sortInfo.getSlotsList().get(i));
                    slotsContent.append(",");
                }
                else
                {
                    slotsContent.append(this.sortInfo.getSlotsList().get(i));
                }
            }
            serverSlots = slotsContent.toString();
            if (serverSlots.equals(""))
            {
                serverSlots = "none";
            }
        }
        catch (Exception e)
        {
            serverSlots = "ex-server-slots";
        }

        return serverSlots;
    }
}
